import { createRouter, createWebHashHistory } from "vue-router";
// 导入路由控制文件
import UserList from "@/views/user/list.vue";
import OrderList from "@/views/order/list.vue";
import CommentList from "@/views/comment/list.vue";
import ImageList from "@/views/image/list.vue";
import NoticeList from "@/views/notice/list.vue";
import SettingBase from "@/views/setting/base.vue";
import CouponList from "@/views/coupon/list.vue";

// 默认路由，所有用户共享
const routes = [
  // 跟页面-->重定向
  // {
  //   path: '/',
  //   redirect: '/home'
  // },

  //   主页
  {
    path: "/",
    name: "admin",
    component: () => import("@/layouts/admin.vue"),
    // 使用动态渲染路由
    // children: [
    //   {
    //     // path: '/home',
    //     path: '/',
    //     component: () => import('@/views/home.vue'),
    //     meta: {
    //       title: '系统首页'
    //     }
    //   },
    //   {
    //     path: '/goods/list',
    //     component: () => import('@/views/goods/list.vue'),
    //     meta: {
    //       title: '商品管理'
    //     }
    //   }
    // ]
  },
  //   登录页
  {
    path: "/login",
    component: () => import("@/views/login.vue"),
    meta: {
      title: "登录页",
    },
  },
  //   404
  {
    path: "/:pathMatch(.*)*",
    name: "NotFound",
    component: () => import("@/views/404.vue"),
    meta: {
      title: "404页面😭",
    },
  },
];

// 动态路由，用于匹配菜单动态添加路由
const asyncRoutes = [
  {
    path: "/",
    name: "/",
    component: () => import("@/views/home.vue"),
    meta: {
      title: "后台首页",
    },
  },
  {
    path: "/goods/list",
    name: "/goods/list",
    component: () => import("@/views/goods/list.vue"),
    meta: {
      title: "商品管理",
    },
  },
  {
    path: "/category/list",
    name: "/category/list",
    component: () => import("@/views/category/list.vue"),
    meta: {
      title: "分类管理",
    },
  },
  {
    path: "/user/list",
    name: "/user/list",
    component: UserList,
    meta: {
      title: "用户列表",
    },
  },
  {
    path: "/order/list",
    name: "/order/list",
    component: OrderList,
    meta: {
      title: "订单列表",
    },
  },
  {
    path: "/comment/list",
    name: "/comment/list",
    component: CommentList,
    meta: {
      title: "评价列表",
    },
  },
  {
    path: "/image/list",
    name: "/image/list",
    component: ImageList,
    meta: {
      title: "图库列表",
    },
  },
  {
    path: "/notice/list",
    name: "/notice/list",
    component: NoticeList,
    meta: {
      title: "公告列表",
    },
  },
  {
    path: "/setting/base",
    name: "/setting/base",
    component: SettingBase,
    meta: {
      title: "配置",
    },
  },
  {
    path: "/coupon/list",
    name: "/coupon/list",
    component: CouponList,
    meta: {
      title: "优惠券列表",
    },
  },
  {
    path: "/antv/list",
    name: "/antv/list",
    component: () => import("@/views/antv/list.vue"),
    meta: {
      title: "拖拽库",
    },
  },
  {
    path: "/antvTwo/list",
    name: "/antvTwo/list",
    component: () => import("@/views/antvTwo/list.vue"),
    meta: {
      title: "AntV拖拽库2",
    },
  },
  {
    path: "/manager/list",
    name: "/manager/list",
    component: () => import("@/views/manager/list.vue"),
    meta: {
      title: "管理员管理",
    },
  },
  {
    path: "/access/list",
    name: "/access/list",
    component: () => import("@/views/access/list.vue"),
    meta: {
      title: "权限管理",
    },
  },
];

export const router = createRouter({
  history: createWebHashHistory(),
  routes,
});
// export default router

// 动态添加路由的方法
export function addRoutes(menus) {
  // 是否有新的路由
  let hasNewRoutes = false;
  const findAndAddRoutesByMenus = (arr) => {
    arr.forEach((e) => {
      let item = asyncRoutes.find((o) => o.path == e.frontpath);
      if (item && !router.hasRoute(item.path)) {
        router.addRoute("admin", item);
        hasNewRoutes = true;
      }
      if (e.child && e.child.length > 0) {
        findAndAddRoutesByMenus(e.child);
      }
    });
  };

  findAndAddRoutesByMenus(menus);

  return hasNewRoutes;
}
